package br.mendonca.intcomp;

import java.io.File;
import java.io.IOException;
import java.util.List;

import br.mendonca.gmmidilib.Music;
import br.mendonca.gmmidilib.enums.KeySignature;
import br.mendonca.gmmidilib.enums.Note;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffSaver;

public class DataSetGenerator {

	private static void generateArffFile(List<Music> musicList) {		
		FastVector attributes = new FastVector();
		for (Note note : Note.values()) {
			attributes.addElement(new Attribute(note.toString()));
		}
	
		//Adding classes - Key Signatures
		FastVector keys = new FastVector(KeySignature.values().length);
		for (KeySignature key : KeySignature.values()) {
			keys.addElement(key.toString());
		}
		attributes.addElement(new Attribute("classes", keys));
		
		Instances data = new Instances("Musics", attributes, 0);
		for (Music music : musicList) {
			Instance instance = createInstance(music);
			data.add(instance);
		}
		
		try {
			ArffSaver saver = new ArffSaver();
			saver.setInstances(data);
			saver.setFile(new File("data/test.arff"));
			saver.writeBatch();
			
		} catch (IOException e) {
			e.printStackTrace();	
		}
	}

	private static Instance createInstance(Music music) {
		float[] musicRatio = music.getNotesRatio();
		double[] attributes = new double[musicRatio.length + 1];
		
		int i;
		for (i = 0; i < musicRatio.length; i++) {
			attributes[i] = (double) musicRatio[i];
		}
		attributes[i] = music.getKeySignature().ordinal();
		
		return new Instance(1.0, attributes);
	}

	public static void main(String[] args) {
		File file = new File(args[0]);
		
		MidiDirectoryCrawler midiDirectoryCrawler = new MidiDirectoryCrawler();
		midiDirectoryCrawler.crawl(file);
		System.out.println(midiDirectoryCrawler.musicList.size());
		
		generateArffFile(midiDirectoryCrawler.musicList);
	}

}
